package hedron.dialog;

import static hedron.i18n.Internationalization._;
import hedron.Application;
import hedron.db.model.Stock;
import hedron.db.model.StockGroup;
import java.util.Arrays;
import java.util.List;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
public class StockDialog extends AbstractDialog {

	List<StockGroup> stockGroups;
	Stock stock;

	Text stockName;
	Text stockCode;
	Combo group;

	public StockDialog(Shell parentShell, Stock stock) {
		super(parentShell);
		this.stock = stock;
		stockGroups = Application.STOCKS.getStockGroups(null);
	}

	@Override
	public void create() {
		super.create();
		if (stock.getID() > 0) {
			setTitle(_("Dialog.Stock.Title.Edit"));
		} else {
			setTitle(_("Dialog.Stock.Title.New"));
		}
		setMessage(_("Dialog.Stock.Message"), 0);
	}

	@Override
	void createContent(Composite parent) {
		Composite grid = new Composite(parent, 0);
		{
			GridLayout layout = new GridLayout();
			layout.numColumns = 2;
			grid.setLayout(layout);
			grid.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
		}

		{
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.BEGINNING, GridData.CENTER, false, false);
			gd.widthHint = 100;
			label.setLayoutData(gd);
			label.setText(_("Dialog.Stock.Label.Name"));
		}
		{
			Text text = new Text(grid, SWT.BORDER);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);
			gd.widthHint = 350;
			text.setLayoutData(gd);
			String defaultValue = stock.STOCK_NAME;
			text.setText(defaultValue);
			text.setSelection(0, defaultValue.length());
			stockName = text;
		}

		{
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.BEGINNING, GridData.CENTER, false, false);
			label.setLayoutData(gd);
			label.setText(_("Dialog.Stock.Label.Barcode"));
		}
		{
			Text text = new Text(grid, SWT.BORDER);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);
			text.setLayoutData(gd);
			text.setText(stock.STOCK_CODE);
			stockCode = text;
		}

		{
			Label label = new Label(grid, 0);
			GridData gd = new GridData(GridData.BEGINNING, GridData.CENTER, false, false);
			label.setLayoutData(gd);
			label.setText(_("Dialog.Stock.Label.StockGroup"));
		}
		{
			Combo combo = new Combo(grid, SWT.BORDER | SWT.READ_ONLY);
			GridData gd = new GridData(GridData.FILL, GridData.CENTER, true, false);
			combo.setLayoutData(gd);
			String[] groups = new String[stockGroups.size()];
			for (int i = 0; i < stockGroups.size(); i++) {
				groups[i] = stockGroups.get(i).GROUP_NAME;
			}
			combo.setItems(groups);
			int selection = Arrays.binarySearch(groups, stock.GROUP_NAME);
			if (selection == -1) {
				selection = 0;
			}
			combo.select(selection);
			group = combo;
		}
	}


	@Override
	protected void okPressed() {
		stock.STOCK_NAME = stockName.getText();
		stock.STOCK_CODE = stockCode.getText();
		stock.GROUP_ID = stockGroups.get(group.getSelectionIndex()).getID();
	}

	@Override
	void createFooter(Composite parent) {
		((GridLayout) parent.getLayout()).numColumns = 1;
		Composite grid = new Composite(parent, 0);
		grid.setLayoutData(new GridData(GridData.FILL, GridData.END, true, false));

		{// set grid layout
			GridLayout layout = new GridLayout();
			layout.numColumns = 3;
			grid.setLayout(layout);
		}
		{// cell 1 - empty text
			Label label = new Label(grid, 0);
			label.setText("");
			GridData gdata = new GridData(GridData.FILL, GridData.END, true, false);
			gdata.heightHint = 20;
			label.setLayoutData(gdata);
			label.setFocus();
		}
		{// cancel
			Button button = new Button(grid, SWT.PUSH);
			button.setText("Cancel");
			GridData gdata = new GridData(GridData.BEGINNING, GridData.END, false, false);
			gdata.heightHint = 20;
			gdata.widthHint = 100;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					setReturnCode(CANCEL);
					close();
				}
			});
		}
		{// ok
			Button button = new Button(grid, SWT.PUSH);
			button.setText("OK");
			GridData gdata = new GridData(GridData.END, GridData.END, false, false);
			gdata.heightHint = 20;
			gdata.widthHint = 100;
			button.setLayoutData(gdata);
			button.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent event) {
					okPressed();
					if (stock.isValid()) {
						setReturnCode(OK);
						close();
					} else {
						setMessage(_("Dialog.Stock.Message"), 2);
					}
				}
			});

			// set button as default
			Shell shell = grid.getShell();
			if (shell != null) {
				shell.setDefaultButton(button);
			}
		}
	}
}
